package driver.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import driver.android.R;
import driver.util.Event;
import driver.util.Util;

public class TaskSummaryDetailListAdapter extends BaseAdapter {
    private Context context;
    private Event event;
    private List<Object> eventDetail = new ArrayList<Object>();
    private static final int TYPE_TOP = 0x0000;
    private static final int TYPE_NORMAL = 0x0001;

    public TaskSummaryDetailListAdapter(Context context, Event ev) {
        this.context = context;
        this.event = ev;
        parseEventDetail();
    }

    private void parseEventDetail() {
        if (this.event != null) {
            String detail = this.event.getDetail();
            try {
                JSONArray jsonSummaryArray = new JSONArray(detail);
                int length = jsonSummaryArray.length();
                for (int index = 0; index < length; index++) {
                    eventDetail.add(jsonSummaryArray.get(index));
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Util.debugApp('d', "getCount异常");
            }
        }
        Util.debugApp('d', "解析详情的长度 " + eventDetail.size());
    }

    @Override
    public int getCount() {
        return eventDetail.size();
    }

    @Override
    public Object getItem(int position) {
        return eventDetail.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_trace, parent, false);
            holder.tvAcceptTime = (TextView) convertView.findViewById(R.id.tvAcceptTime);
            holder.tvAcceptStation = (TextView) convertView.findViewById(R.id.tvAcceptStation);
            holder.tvTopLine = (TextView) convertView.findViewById(R.id.tvTopLine);
            holder.tvDot = (TextView) convertView.findViewById(R.id.tvDot);
            convertView.setTag(holder);
        }
        if (getItemViewType(position) == TYPE_TOP) {
            // 第一行头的竖线不显示
            holder.tvTopLine.setVisibility(View.INVISIBLE);
            // 字体颜色加深
            holder.tvAcceptTime.setTextColor(0xff999999);
            holder.tvAcceptStation.setTextColor(0xff999999);
            holder.tvDot.setBackgroundResource(R.drawable.timelline_dot_first);
        } else if (getItemViewType(position) == TYPE_NORMAL) {
            holder.tvTopLine.setVisibility(View.VISIBLE);
            holder.tvAcceptTime.setTextColor(0xff555555);
            holder.tvAcceptStation.setTextColor(0xff555555);
            holder.tvDot.setBackgroundResource(R.drawable.timelline_dot_normal);
        }
        String json = eventDetail.get(position).toString();
        String item = Util.getJsonKeyValue("item", json) == null ? "" : Util.getJsonKeyValue("item", json);
        String value = Util.getJsonKeyValue("value", json) == null ? "" : Util.getJsonKeyValue("value", json);
        holder.tvAcceptTime.setText(item);
        holder.tvAcceptStation.setText(value);
        return convertView;
    }

    @Override
    public int getItemViewType(int id) {
        if (id == 0) {
            return TYPE_TOP;
        }
        return TYPE_NORMAL;
    }

    static class ViewHolder {
        public TextView tvAcceptTime, tvAcceptStation;
        public TextView tvTopLine, tvDot;
    }
}
